package Messages;

import java.util.HashMap;

public abstract class Message
{
	protected String m_type = "";
	protected String m_context = "";
	
	protected HashMap<String, String> m_varList = new HashMap<String, String>();
	protected String[] m_renderOrder = new String[0];
	
	public String GetVar(String varName)
	{
		return m_varList.get(varName);
	}
	
	public String RenderMessage()
	{
		String message = m_type + ":" + m_context;
		for(int i = 0; i < m_renderOrder.length; i++)
		{
			message += "," + GetVar(m_renderOrder[i]);
		}
		message += ";";
		return message;
	}
	
	protected void PutStringInVarList(String messageVarList) throws InvalidVarListException
	{
		PutStringInVarList(messageVarList, 0);
	}
	
	protected void PutStringInVarList(String messageVarList, int startIndex) throws InvalidVarListException
	{
		messageVarList.trim();
		if(messageVarList.startsWith(","))
			messageVarList = messageVarList.substring(1);
		if(messageVarList.endsWith(";"))
			messageVarList = messageVarList.substring(0, messageVarList.length() - 1);
		String[] varArray = messageVarList.split(",");
		if(varArray.length != m_renderOrder.length)
			throw new InvalidVarListException();
		for(int i = startIndex; i < varArray.length; i++)
		{
			this.m_varList.put(m_renderOrder[i], varArray[i]);
		}
	}
}
